<?php

/**
 * This is the model class for table "{{company_certificate}}".
 *
 * The followings are the available columns in table '{{company_certificate}}':
 * @property integer $id
 * @property string $name
 * @property string $grantDate
 * @property string $verifyDate
 * @property string $extendDate
 * @property string $expireDate
 * @property string $previewImage
 * @property string $previewThumb
 * @property string $note
 * @property string $createTime
 * @property integer $createUserId
 * @property string $updateTime
 * @property integer $updateUserId
 *
 * The followings are the available model relations:
 * @property User $updateUser
 * @property User $createUser
 */
class CompanyCertificate extends CActiveRecord {
	/**
	 * Returns the static model of the specified AR class.
	 * @return CompanyCertificate the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{company_certificate}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'name',
				'required'
			),
			array(
				'name, previewImage, previewThumb',
				'length',
				'max' => 255
			),
			array(
				'previewImage',
				'file',
				'types' => 'jpg, gif, png',
				'allowEmpty' => true,
			),
			array(
				'previewImage',
				'unsafe'
			),
			array(
				'grantDate, verifyDate, extendDate, expireDate',
				'date',
				'format' => 'yyyy-MM-dd',
			),
			array(
				'grantDate, verifyDate, extendDate, expireDate',
				'default',
				'value' => null,
				'setOnEmpty' => true
			),
			array(
				'note',
				'safe'
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, name, grantDate, verifyDate, extendDate, expireDate, previewImage, previewThumb, note, createTime, createUserId, updateTime, updateUserId',
				'safe',
				'on' => 'search'
			),
		);
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'name, grantDate, verifyDate, extendDate, expireDate, previewImage, previewThumb, note'
			),
			'AuditStampBehavior' => array(
				'class' => 'components.db.AuditStampBehavior',
			),
			'CertificatePhotoUploadBehavior' => array(
				'class' => 'components.db.ProcessUploadImageBehavior',
				'attribute' => 'previewImage',
				'imageWidth' => 800,
				'imageHeight' => 600,
				'imageFolder' => 'companyCertificate',
				'thumbAttribute' => 'previewThumb',
				'thumbWidth' => 160,
				'thumbHeight' => 120,
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'updateUser' => array(
				self::BELONGS_TO,
				'User',
				'updateUserId'
			),
			'createUser' => array(
				self::BELONGS_TO,
				'User',
				'createUserId'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'name' => '资质名称',
			'verifyDate' => '年检时间',
			'extendDate' => '延续时间',
			'expireDate' => '到期时间',
			'grantDate' => '获证日期',
			'note' => '备注',
			'previewImage' => '图片预览',
			'previewThumb' => '缩略图',
			'createTime' => '创建时间',
			'createUserId' => '创建用户',
			'updateTime' => '更新时间',
			'updateUserId' => '更新用户',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('name', $this->name, true);
		$criteria->compare('grantDate', $this->grantDate, true);
		$criteria->compare('verifyDate', $this->verifyDate, true);
		$criteria->compare('extendDate', $this->extendDate, true);
		$criteria->compare('expireDate', $this->expireDate, true);
		$criteria->compare('previewImage', $this->previewImage, true);
		$criteria->compare('previewThumb', $this->previewThumb, true);
		$criteria->compare('note', $this->note, true);
		$criteria->compare('createTime', $this->createTime, true);
		$criteria->compare('createUserId', $this->createUserId);
		$criteria->compare('updateTime', $this->updateTime, true);
		$criteria->compare('updateUserId', $this->updateUserId);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}
}
